package tazadum.engine.event.sync;

import tazadum.engine.event.SyncEventChannel;

public class BeforeSyncEvent implements SyncEvent {
	private final SyncEventChannel channel;
	private final float length;

	public BeforeSyncEvent(final SyncEventChannel channel, final float length) {
		this.channel = channel;
		this.length = length;

		assert length > 0 : "Length of an event must be greater than zero";
	}

	@Override
	public float evaluate(final float time) {
		final float event = channel.get(time);
		if (time > event) {
			return 1f;
		}
		if (time < event - length) {
			return 0f;
		}
		return (event - time) / length;
	}
}
